import math
import random

id_len = 7


class DataModel:
    def __init__(self):
        self._parent = {'Root': {'@Application': 'WoodMachine', '@ApplicationVersion': '2.0', '@version': 'V2.37f',
                                 '@xmlns:xsi': 'https://www.w3.org/2001/XMLSchema-instance',
                                 '@xmlns:xsd': 'https://www.w3.org/2001/XMLSchema',
                                 'Cabinet': []
                                 # 'Cabinet': [Cabinet()]
                                 }
                        }
        self.cabinet = []

    @property
    def myResult(self):
        for cabinet_index in range(len(self.cabinet)):
            self._parent['Root']['Cabinet'].append(self.cabinet[cabinet_index].myResult())
        return self._parent

    def __len__(self):
        return len(self.cabinet)


class Cabinet:
    def __init__(self):
        self.Name = "未命名"
        self.Length = "1700"
        self.Width = "4340"
        self.Height = "2730"
        self.Material = "无"
        self.BasicMaterial = "无"
        self.Model = "0"
        self.GroupName = "未命名"
        self.RoomName = "未命名"
        self.OrderDate = "2019-08-28"
        self.OrderNo = ""
        self.Customer = ""
        self.ShopName = "丽维家门店"
        self.Designer = ""  # 设计师
        self.OrderName = ""
        self.panels = []
        self.metals = []
        # self.panels = [Panel()]

    def myResult(self):
        for panel_index in range(len(self.panels)):
            self.panels[panel_index] = self.panels[panel_index].myResult()
        for metals_index in range(len(self.metals)):
            self.metals[metals_index] = self.metals[metals_index].myResult()
        return {
            '@Name': str(self.Name),
            '@Length': str(self.Length),
            '@Width': str(self.Width),
            '@Height': str(self.Height),
            '@Material': str(self.Material),
            '@BasicMaterial': str(self.BasicMaterial),
            '@Designer': str(self.Designer),
            '@Model': str(self.Model),
            '@GroupName': str(self.GroupName),
            '@RoomName': str(self.RoomName),
            '@OrderDate': str(self.OrderDate),
            '@OrderNo': str(self.OrderNo),
            '@Customer': str(self.Customer),
            '@ShopName': str(self.ShopName),
            '@OrderName': str(self.OrderName),
            'Panels': {'Panel': self.panels},
            'Metals': {'@discard': True, 'Metal': self.metals}
        }

    def __len__(self):
        return len(self.panels)


class Panel:
    def __init__(self):
        self.Grain = "V"  # 纹理方向 H横纹 V竖纹
        self._thickness = "0"
        self.Length = "0"
        self.Width = "0"
        self.ID = ""
        self.Name = "避梁右侧板"
        self.Material = "实木樱桃"
        self.edgeMaterial = "实木樱桃"
        self.MachiningPoint = "5"
        self.BasicMaterial = "实木多层"
        self.ActualLength = "0"
        self.ActualWidth = "0"
        self.Category = "Board"
        self.Face5ID = ""  # 正面条码
        self.Face6ID = ""  # 反面条码
        self.EdgeGroup = EdgeGroup()
        self.machine = []
        # self.machine = [Machining()]

    @property
    def Thickness(self):
        return self._thickness

    @Thickness.setter
    def Thickness(self, thickness):
        self._thickness = int(float(thickness))

    def myResult(self):
        for machine_index in range(len(self.machine)):
            self.machine[machine_index] = self.machine[machine_index].myResult()
        return {
            '@Grain': str(self.Grain),
            '@Thickness': str(self.Thickness),
            '@Length': str(self.Length),
            '@Width': str(self.Width),
            '@ID': str(self.ID).zfill(id_len),
            '@Name': str(self.Name),
            '@Face5ID': str(self.Face5ID),
            '@Face6ID': str(self.Face6ID),
            '@Material': str(self.Material),
            '@edgeMaterial': str(self.edgeMaterial),
            '@MachiningPoint': str(self.MachiningPoint),
            '@BasicMaterial': str(self.BasicMaterial),
            '@ActualLength': str(self.ActualLength),
            '@ActualWidth': str(self.ActualWidth),
            '@Category': str(self.Category),
            'EdgeGroup': self.EdgeGroup.myResult(),
            'Machines': {'Machining': self.machine}
        }

    def __len__(self):
        return len(self.machine)


class EdgeGroup:
    def __init__(self):
        self.X1 = "0"
        self.Y1 = "0"
        self.edges = []
        # self.edges = [Edge()]

    def myResult(self):
        for edge_index in range(len(self.edges)):
            self.edges[edge_index] = self.edges[edge_index].myResult()
        return {
            '@X1': str(self.X1),
            '@Y1': str(self.Y1),
            'Edge': self.edges
        }

    def __len__(self):
        return len(self.edges)


class Edge:
    def __init__(self):
        self.Face = "1"
        self.LindID = "1"
        self._Thickness = 0
        self.Length = "330"

    @property
    def Thickness(self):
        return self._Thickness

    @Thickness.setter
    def Thickness(self, thickness):
        if float(thickness) < 0:
            self._Thickness = 0
        else:
            self._Thickness = float(thickness)

    def myResult(self):
        return {
            '@Face': str(self.Face),
            '@LindID': str(self.LindID),
            '@Thickness': str(self._Thickness),
            '@Length': str(self.Length)
        }

    def __neg__(self):
        return self._Thickness


class Machining:
    """
    Type 代表板件的处理方式  1水平孔 2 垂直孔 3开槽
    Face 代表板件的面   # 1后面 2前面 3右面 4左面 5上面 6下面
    """

    def __init__(self):
        self.ID = random.randint(10, 99999)
        self.Type = ""  # 1水平孔 2 垂直孔 3 开槽
        self.Face = "5"  # 1后面 2前面 3右面 4左面 5上面 6下面
        self._X = "0.00"  # 坐标
        self._Y = "0.00"
        self._Z = "0.00"
        self.HoleType = "0"
        self.Diameter = "0.00"  # 孔直径
        self.Depth = "0.00"  # 孔深度
        self.Width = ""  # 槽宽
        self.IsGenCode = "2"  # 类型
        self.ToolName = ""  # 刀具
        self.ToolOffset = ""
        self.EdgeABThickness = ""  # 如果轮廓就需要带上这个，拉槽不用
        self.GrooveType = "0"  # 线框类型 0外部轮廓 1内部轮廓
        self.lines = []
        # self.lines = [Line()]
        # wire_type这是自定义的属性，用来最后确定线条类型，最后不会输出到xml,作为内部使用的方法
        # 定义： 0 为孔(默认) 1 为槽 2 为内部框线 3 为外部框线
        self.wire_type = 0

    @property
    def Y(self):
        return self._Y

    @Y.setter
    def Y(self, y):
        if float(y) < 0:
            self._Y = 0
        else:
            self._Y = float(y)

    @property
    def X(self):
        return self._X

    @X.setter
    def X(self, x):
        if float(x) < 0:
            self._X = 0
        else:
            self._X = float(x)

    @property
    def Z(self):
        return self._Z

    @Z.setter
    def Z(self, z):
        if float(z) < 0:
            self._Z = 0
        else:
            self._Z = float(z)

    def myResult(self):
        for lines_index in range(len(self.lines)):
            self.lines[lines_index] = self.lines[lines_index].myResult()
        res = {'@ID': str(self.ID).zfill(6),
               '@IsGenCode': str(self.IsGenCode),
               '@Type': str(self.Type),
               '@Face': str(self.Face),
               '@X': str(self._X),
               '@Y': str(self._Y),
               '@Z': str(self._Z),
               '@HoleType': str(self.HoleType),
               '@Diameter': '%.2f' % float(self.Diameter),
               '@Depth': str(self.Depth)
               # '@EdgeABThickness': str(self.EdgeABThickness),
               # '@ToolName': str(self.ToolName),
               # '@ToolOffset': str(self.ToolOffset)
               }

        if self.wire_type != 0:
            res['@ToolName'] = str(self.ToolName)
            res['@ToolOffset'] = str(self.ToolOffset)
            res['Lines'] = {'Line': self.lines}
            if self.wire_type == 1:
                res['@Width'] = str(self.Width)
            else:
                res['@EdgeABThickness'] = str(self.EdgeABThickness)
                res['@GrooveType'] = str(self.GrooveType)
        return res

    def __len__(self):
        return len(self.lines)


class Line:
    def __init__(self):
        self.LineID = "1"
        self._EndX = "0.00"
        self._EndY = "0.00"
        self.Angle = "0.00"
        self.Thickness = "0"
        self.edge = Edge

    @property
    def EndY(self):
        return self._EndY

    @EndY.setter
    def EndY(self, y):
        if float(y) < 0:
            self._EndY = 0
        else:
            self._EndY = float(y)

    @property
    def EndX(self):
        return self._EndX

    @EndX.setter
    def EndX(self, x):
        if float(x) < 0:
            self._EndX = 0
        else:
            self._EndX = float(x)

    def myResult(self):
        return {
            '@LineID': str(self.LineID),
            '@EndX': str(self._EndX),
            '@EndY': str(self._EndY),
            '@Angle': str(self.Angle),
            '@Thickness': str(self.Thickness)
        }


class Metal:
    def __init__(self):
        self.Id = "00358233"
        self.Name = "自攻丝"
        self.PartNumber = "ZGS"
        self._length = "0"
        self.width = "0"
        self.height = "0"
        self.Num = "4"
        self.Unit = "个"

    @property
    def length(self):
        return self._length

    @length.setter
    def length(self, length):
        if length:
            self._length = length
        else:
            self._length = 0

    def myResult(self):
        return {
            '@Id': str(self.Id),
            '@Name': str(self.Name),
            '@PartNumber': str(self.PartNumber),
            '@length': str(self.length),
            '@height': str(self.height),
            '@Num': str(self.Num),
            '@width': str(self.width)
        }
